package com.ed100.sass.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ed100.sass.model.po.Account;
import com.ed100.sass.service.AccountService;

@Controller
@Configurable
@ImportResource(locations={"classpath:spring/*.xml"})
@SpringBootApplication(scanBasePackages= {"com.ed100.sass"})
public class SampleController {

	@Autowired
	private AccountService accountService;

	// 从 application.properties 中读取配置，如取不到默认值为Hello Shanhy
	@Value("${test.name:Hello Shanhy}")
	private String hello = "aaaa";

	@RequestMapping("/")
	public @ResponseBody String home() {

		return hello;
	}

	@RequestMapping("/list_db")
	public @ResponseBody List<Account> list() {

		return accountService.queryAll();
	}
	
	@RequestMapping("update_db")
	public @ResponseBody boolean update() {
		
		return accountService.update();
	}

	@RequestMapping("/list")
	public List<Map<String, String>> getList() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = null;
		for (int i = 1; i <= 5; i++) {
			map = new HashMap<String, String>();
			map.put("name", "Shanhy-" + i);
			list.add(map);
		}
		return list;
	}
	
	@RequestMapping("page")
	public String page1(Map<String,Object> data) {
		data.put("toUserName", "用户A");
		data.put("message", "This is Spring-boot Worlds!!");
		data.put("fromUserName", "用户B");
		data.put("time", new Date());
		
		return "test";
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleController.class, args);
	}
}
